<?php
namespace App\Http\Controllers\Index;

use App\Http\Responses\DemoResp;
use App\Logic\Sms;
use App\Logic\Code;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User as userModel;

class InviteController extends Controller
{
    /**
     * @OA\Post(
     *     path="/regional",
     *     tags={"邀请"},
     *     summary="邀请信息",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"type"},
     *                 @OA\Property(property="type", type="string", description="大区：big 小区：small"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function regional(Request $request)
    {
        $post_data = $request->all();

        $validateRule = [
            'type'                      => 'required',
        ];
        $errorMsg = [
            'type.required'                 => '请传入类型',
        ];

        $validator = Validator::make($post_data, $validateRule, $errorMsg);
        if ($validator->fails()){
            $errors = $validator->errors()->all();
            return e_msg(300001,$errors[0]);
        }

        //获取此人直推，，，直推分别的业绩   取大区 取小区
        $direct = DB::table('user')->where('pid',$this->user['id'])->get()->toArray();

        if(!$direct){


            $arr =  [
                'big_inviter_count' => 0,
                'small_inviter_count' => 0,
                'big_performance' => 0,
                'small_performance' => 0,
                'big_capacity' => 0,
                'small_capacity' => 0,
                'big' => [],
                'small' => [],
            ];
            return s_msg('',$arr);

        }

        $arr = array_column($direct,'capacity');

        $max = max($arr);
        $key = array_keys($arr,$max)[0];
        $da = $direct[$key];
        unset($direct[$key]);
        $xiao[] = $direct;
        $big[] = $da;

        $all_count = DB::table('user')
            ->where('path','like',$this->user['path'].$this->user['id'].',%')
            ->count();
        $da_count =DB::table('user')
            ->where('path','like',$da->path.$da->id.',%')
            ->count()+1;
        $xiao_count = $all_count - $da_count;

        $xiao_capacity_arr = array_column($direct,'capacity');
        $xiao_capacity = array_sum($xiao_capacity_arr);

        $xiao_performance_arr = array_column($direct,'performance');
        $xiao_performance = array_sum($xiao_performance_arr);
        $arr = [
            'big_inviter_count' => $da_count,       //大区数量
            'small_inviter_count' => $xiao_count,      //小区数量
            'big_performance' => $da->performance,      //大区业绩
            'small_performance' => $xiao_performance,     //小区业绩
            'big_capacity' => $da->capacity,            //大区算力
            'small_capacity' => $xiao_capacity,         //小区算力
            'big' => $big,
            'small' => $xiao,
        ];

        return s_msg('',$arr);

    }



    /**
     * @OA\Post(
     *     path="/direct",
     *     tags={"邀请"},
     *     summary="推荐列表",
     *      @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"user_id"},
     *                 @OA\Property(property="user_id", type="string", description="用户id"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function direct(Request $request)
    {
        if($request->has('user_id')){
            $order_info = Db::table('user')
                ->where('id',$request->input('user_id'))
                ->first();
            if($order_info){
                $direct = DB::table('user')->where('pid',$request->input('user_id'))
                    ->select('phone','state','capacity','add_time')
                    ->paginate(15);
                return s_msg('',$direct);
            }else{
                return e_msg(300020,'用户信息不存在');
            }


        }else{
            return e_msg(300019,'未传入用户id');
        }

    }





}
